package com.shangxian.giveithome.service.impl;


import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.entity.PageableDetails;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.community.AttentionUserList;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.AttentionMapper;
import com.shangxian.giveithome.mapper.CollectionsMapper;
import com.shangxian.giveithome.mapper.IssueImgMapper;
import com.shangxian.giveithome.mapper.IssueMapper;
import com.shangxian.giveithome.service.MyCommunityService;
import com.shangxian.giveithome.utils.PageUtils;
import com.shangxian.giveithome.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class MyCommunityServiceImpl implements MyCommunityService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private AttentionMapper attentionMapper;

    @Autowired
    private IssueImgMapper issueImgMapper;

    @Autowired
    private IssueMapper issueMapper;

    @Autowired
    private CollectionsMapper collectionsMapper;

    @Autowired
    private RedisUtils redisUtils;


    /**
     * 查询用户动态的个人信息
     * @param request
     * @param id
     * @return
     */
    @Override
    public AttentionUserList findIssueUserMessige(HttpServletRequest request,Long id) {
        if (redisUtils.getHeader(request) ==null && id ==null){
            ExceptionUtils.error(CommonCode.PARAMETER_ERROR);
        }
        if (id !=null){
            int count = attentionMapper.findUserExist(id);
            if (count<1){
                ExceptionUtils.error(CommonCode.DATA_ERROR);
            }
            AttentionUserList mine = attentionMapper.findUserMessage(id);
        if (redisUtils.getHeader(request) !=null) {
            UserDetails userDetails = redisUtils.getHeader(request);
            if (id.equals(userDetails.getId())) {
                mine.setState(2);
            } else {
                int i = attentionMapper.findAttentionState(userDetails.getId(), id);
                if (i > 0) {
                    mine.setState(1);
                    return mine;
                }
            }
        }
        if (mine.getName() ==null){
            mine.setName("");
        }
        if (mine.getPicture() ==null){
            mine.setPicture("");
        }
            return mine;
        }
        UserDetails userDetails = redisUtils.getHeader(request);
        AttentionUserList mine = attentionMapper.findUserMessage(userDetails.getId());
        if (mine.getId() == null){
            return new AttentionUserList();
        }
        mine.setState(2);
        return mine;
    }















































    /**
     * 返回
     *
     * @param pageNumber 当前页
     * @param pageSize   每页条数
     * @param count      总条数
     * @param list       返回数据
     * @param <T>        泛型
     * @return
     */
    public <T> PageableDetails returnPageable(int pageNumber, Integer pageSize, Integer count, List<T> list) {
        return new PageableDetails<>(pageNumber, pageSize, count, list);
    }


    /**
     * 返回总页数
     *
     * @param total
     * @param pageSize
     * @return
     */
    public int returnPages(int total, int pageSize) {
        return PageUtils.returnPages(total, pageSize);
    }
}
